<?php

namespace app\upload\controller;

use app\upload\service\UploadService;
use think\Db;

/**
 * @title 纪念日模块
 * @description 纪念日的所有功能类
 * @group 纪念日
 */
class Pic extends UploadService
{
	/**
     * @title 上传纪念日图片
     * @description 上传纪念日图片
     * @author 胡天宇
     * @url /upload/Pic/uploadMarkdayPic
     * @method post
     * @param name:markdayid type:int require:1 desc:纪念日id
     * @param name:imagefile type:Varchar require:1  desc:图片
     * @return name:code：状态码
     * @return name:msg：状态消息
     */
	public function uploadMarkdayPic()
	{
		$param  = request()->param();

		$file = request()->file('imagefile');

		if (empty($param['markdayid']))
		{
			return json(['code' => 401, 'msg' => '参数为空', 'data' => []]);
		}

		if ($file)
		{
			$res = $this->addMarkdayPic($file);
		}
		else
		{
			return json(['code' => 401, 'msg' => '请选择图片', 'data' => []]);
		}

		if (200 == $res['code'])
		{
			$param['picImg'] = $res['imagepath'];
			unset($param['/upload/Pic/uploadMarkdayPic']);

			// 图片上传成功后，插入一条记录
			$res = $this->addMarkdayPicRow($param);	

			if ($res)
			{
				return json(['code' => 200, 'msg' => '成功', 'data' => ['imgPath' => $param['picImg']]]);
			}
			else
			{
				return json(['code' => 402, 'msg' => '图片上传成功，记录插入失败', 'data' => []]);
			}
		}	
		else
		{
			return json(['code' => 402, 'msg' => '图片上传失败', 'data' => []]);
		}
	}

	// 获取该纪念日下的图片
	public function getMarkdayPic()
	{
		$param = request()->param();

		if (empty($param['markdayid']) || empty($param['type']))
		{
			return json(['code' => 401, 'msg' => '参数为空', 'data' => []]);
		}

		$res = $this->markdayPic($param);

		if ($res)
		{
			return json(['code' => 200, 'msg' => '成功', 'data' => $res]);
		}
		else
		{
			return json(['code' => 402, 'msg' => '图片为空', 'data' => []]);
		}
	}
	
	// 得到纪念日id
	public function getMarkdayId()
	{
		$key = Db::name('markday')
			   ->order('markdayid desc')
			   ->value('markdayid');

		return json(['data' => ['key' => $key+1]]);
	}

	/**
     * @title 上传纪念日背景图片
     * @description 上传纪念日背景图片
     * @author 胡天宇
     * @url /upload/Pic/uploadMarkdayBGPic
     * @method post
     * @param name:markdayid type:int require:1 desc:纪念日id
     * @param name:imagefile type:Varchar require:1  desc:图片
     * @return name:code：状态码
     * @return name:msg：状态消息
     */
	public function uploadMarkdayBGPic()
	{
		$param  = request()->param();

		$file = request()->file('imagefile');

		if (empty($param['markdayid']))
		{
			return json(['code' => 401, 'msg' => '参数为空', 'data' => []]);
		}

		if ($file)
		{
			$res = $this->addMarkdayPic($file);
		}
		else
		{
			return json(['code' => 401, 'msg' => '请选择图片', 'data' => []]);
		}

		if (200 == $res['code'])
		{
			$param['backgroundpicImg'] = $res['imagepath'];
			unset($param['/upload/Pic/uploadMarkdayBGPic']);
			// 图片上传成功后，插入一条记录

			$res = $this->addMarkdayBGPicRow($param);	

			if ($res)
			{
				return json(['code' => 200, 'msg' => '成功', 'data' => ['imgPath' => $param['backgroundpicImg']]]);
			}
			else
			{
				return json(['code' => 402, 'msg' => '图片上传成功，记录插入失败', 'data' => []]);
			}
		}	
		else
		{
			return json(['code' => 402, 'msg' => '图片上传失败', 'data' => []]);
		}
	}

	public function test()
	{
		return $this->fetch('/test');
	}

	public function test1()
	{
		return $this->fetch('/test1');
	}
}